﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Xml;
using PTUDW_CNTN2010_F_CDT.ViewModel;
using PTUDW_CNTN2010_F_CDT.Filters;

namespace PTUDW_CNTN2010_F_CDT.Controllers
{
    [LanguageFilter]
    public class ListProductController : Controller
    {
        //
        // GET: /DanhMucSP/

        public ActionResult Index()
        {
            return View();
        }
        bool flag = false;

        String[] DM = {"Điện thoại", "Máy nghe nhạc", "Loa - tai nghe"};
        List<ListProduct> DanhMuc;

        [ChildActionOnly]
        public ActionResult getListProduct()
        {
            List<ListProduct> list = getData();
            return PartialView(list);
        }

        public ActionResult ListProduct(ListProduct list)
        {
            List<Product> lstProduct = getProduct();
            List<Product> lstPro = new List<Product>();
            foreach (Product p in lstProduct)
                if (p.ListID == list.Id)
                    lstPro.Add(p);
            return View(lstPro);
        }

        Product p;
        private List<Product> getProduct()
        {

            List<Product> lstProduct = new List<Product>();

            XmlTextReader xmlr = new XmlTextReader(Server.MapPath(@"..\..\App_Data\data.xml"));
            if (xmlr != null)
            {
                while (xmlr.Read())
                {
                    if (!flag)
                        p = new Product();
                    if (xmlr.NodeType == XmlNodeType.Element)
                    {
                        if (xmlr.LocalName.Equals("id"))
                        {
                            flag = true;
                            p.Id = int.Parse(xmlr.ReadString());
                        }
                        if (xmlr.LocalName.Equals("name"))
                            p.Name = xmlr.ReadString();
                        if (xmlr.LocalName.Equals("price"))
                            p.Price = int.Parse(xmlr.ReadString());
                        if (xmlr.LocalName.Equals("path"))
                            p.pathPhoto = xmlr.ReadString();
                        if (xmlr.LocalName.Equals("idlist"))
                        {
                            p.ListID = int.Parse(xmlr.ReadString());
                            flag = false;
                            lstProduct.Add(p);
                        }
                    }
                }
            }
            return lstProduct;
        }

        private List<ListProduct> getData()
        {
            DanhMuc = new List<ListProduct>();
            for (int i = 0; i < DM.Count(); i++)
            {
                ListProduct p = new ListProduct();
                p.Id = i;
                p.Name = DM[i];
                DanhMuc.Add(p);
            }
            return DanhMuc;
        }
    }
}
